package com.zmn.plat.services.impl.product;

import com.zmn.common.dto.StatusDTO;
import com.zmn.plat.model.entity.product.ServProductRelation;
import com.zmn.plat.model.entity.product.ServProductRelationQuery;
import com.zmn.plat.model.vo.product.relation.ServProductRelationVO;
import com.zmn.plat.persistence.interfaces.product.ServProductRelationDAO;
import com.zmn.plat.services.interfaces.product.ServProductRelationService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author tanbiao
 * @description 产品关联关系
 * @modifier
 * @since 2019/9/20 11:46
 **/
@Service
public class ServProductRelationServiceImpl implements ServProductRelationService {
    @Resource
    ServProductRelationDAO servProductRelationDAO;

    @Override
    public ServProductRelation findByProductId(Integer productId) {
        ServProductRelationQuery relationQuery = new ServProductRelationQuery();
        relationQuery.setProductId(productId);
        List<ServProductRelation> relations = listByQuery(relationQuery);
        if (!CollectionUtils.isEmpty(relations)) {
            return relations.get(0);
        }
        return null;
    }

    @Override
    public List<ServProductRelation> listPageByQuery(ServProductRelationQuery query) {
        return servProductRelationDAO.listPageByQuery(query);
    }

    @Override
    public List<ServProductRelation> listByQuery(ServProductRelationQuery query) {
        return servProductRelationDAO.listByQuery(query);
    }

    @Override
    public List<ServProductRelationVO> listByQueryForShow(ServProductRelationQuery query) {
        return servProductRelationDAO.listByQueryFowShow(query);
    }

    @Override
    public List<ServProductRelationVO> listByProductIds(List<Integer> productIds) {

        return servProductRelationDAO.listByProductIds(productIds);
    }

    @Override
    public List<ServProductRelationVO> listShowProductByProductIds(List<Integer> productIds) {
        return servProductRelationDAO.listShowProductByProductIds(productIds);
    }

    @Override
    @Cacheable(cacheNames = "redis5m", key = "'product:ids:'+#p0", unless = "#result == null")
    public List<Integer> listProductIdByServProductStatus(Integer status) {

        if (Objects.isNull(status)) {
            return Collections.emptyList();
        }
        return servProductRelationDAO.listProductIdByServProductStatus(status);
    }
    @Override
    @Cacheable(cacheNames = "redis5m", key = "'product:ids:'+#p0", unless = "#result == null")
    public List<Integer> listProductIdByServProductBizType(Integer bizType) {

        if (Objects.isNull(bizType)) {
            return Collections.emptyList();
        }
        return servProductRelationDAO.listProductIdByServProductBizType(bizType);
    }

    @Override
    public List<Integer> listProductIdByChannelIdAndStatus(Integer channelId, Integer status) {

        if (Objects.isNull(channelId) || Objects.isNull(status)) {
            return Collections.emptyList();
        }
        return servProductRelationDAO.listProductIdByChannelIdAndStatus(channelId, status);
    }

    @Override
    public Integer countByQuery(ServProductRelationQuery query) {

        return servProductRelationDAO.countByQuery(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateByKey(ServProductRelation query) {
        return servProductRelationDAO.updateByKey(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateStatusBatch(List<Integer> arrayIds, int status, String mcStaffName) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(arrayIds);
        dto.setStatus(status);
        dto.setUpdater(mcStaffName);
        return servProductRelationDAO.updateStatusBatch(dto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateBatch(ServProductRelation relation, List<Integer> exitsProductIds) {
        return servProductRelationDAO.updateBatch(relation, exitsProductIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insert(ServProductRelation relation) {
        return servProductRelationDAO.insert(relation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertBatch(List<ServProductRelation> insertRelations) {
        return servProductRelationDAO.insertBatch(insertRelations);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteBatch(List<Integer> ids) {
        return servProductRelationDAO.deleteBatch(ids);
    }

}
